import { useUserStore } from '@/stores/user'

const userStore = useUserStore()

/**
 * 验证权限是否存在
 * @param auth
 * @param authGroup
 */
export const validator = (auth, authGroup = false) => {
  let keys = Object.keys(auth)
  let result = {}
  const data = userStore.authorities || []
  keys.forEach(key => {
    result[key] = data.includes(auth[key])
  })
  result.isContainAuth = Object.values(result).includes(true)

  if (authGroup) {
    let groupKeys = Object.keys(authGroup)
    let groupRes = {}
    groupKeys.forEach(key => {
      let newArr = authGroup[key].map(name => {
        return result[name]
      })
      groupRes[key] = Object.values(newArr).includes(true)
    })
    result.groupAuth = groupRes
  }
  return result
}
